﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using EAGL.Data;

namespace EAGL.Explore
{
    public class FeatureCursorExplorer
    {
        private IFeatureCursor featureCursor;
        
        public FeatureCursorExplorer(IFeatureCursor featureCursor)
        {
            this.featureCursor = featureCursor;
        }

        private List<object[]> getUniqueValues(List<string> fieldNames)
        {
            Dictionary<string, int> mappings = FieldMapper.GetMappings(this.featureCursor.Fields, fieldNames);
            IFeature feature = null;
            int c = fieldNames.Count;
            int i = 0;
            object[] current_values = new object[c];
            List<object[]> uvalues = new List<object[]>();

            while ((feature = featureCursor.NextFeature()) != null)
            {
                i = 0;
                while (i < c)
                {
                    current_values[i] = feature.get_Value((int)mappings[fieldNames[i]]);
                    i++;
                }

                bool v = ObjectArrayComparer.IsIn(current_values, uvalues);

                if (!v)
                {
                    uvalues.Add((object[])current_values.Clone());
                }

            }
            return uvalues;
        }

        private List<string> getUniqueValues(string fieldName)
        {
            List<string> uvalues = new List<string>();
            int fieldIndex = featureCursor.FindField(fieldName);
            IFeature feature = null;
            
            
            while ((feature = featureCursor.NextFeature()) != null)
            {
                string s = feature.get_Value(fieldIndex).ToString();
                if (!uvalues.Contains(s))
                    uvalues.Add(s);  
            }


            var v = from item in uvalues orderby item select item; 
            return v.ToList<string>();
        }

        private List<string> getUniqueValues2(string fieldName)
        {
            HashSet<string> uvalues = new HashSet<string>();
            int fieldIndex = featureCursor.FindField(fieldName);
            IFeature feature = null;


            while ((feature = featureCursor.NextFeature()) != null)
            {
                uvalues.Add(feature.get_Value(fieldIndex).ToString());
            }
            var v = from item in uvalues orderby item select item;
            return v.ToList<string>();
        }


        /// <summary>
        /// Rreturns a list of unique values of field(s) in a feature class
        /// </summary>
        /// <param name="fieldNames"></param>
        /// <returns></returns>
        public List<object[]> GetUniqueValues(string[] fieldNames)
        {
            return this.getUniqueValues(fieldNames.ToList<string>());
        }

        
        /// <summary>
        /// Rreturns a list of unique values of one field in a feature class
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public List<string> GetUniqueValues(string fieldName)
        {
            return this.getUniqueValues(fieldName);
        }

        /// <summary>
        /// Rreturns a list of unique values of one field in a feature class (Another method, needs benchmarking)
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public List<string> GetUniqueValues2(string fieldName)
        {
            return this.getUniqueValues2(fieldName);
        }


    }
}
